//
//  HotchpotchReplyDetailVC.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/24.
//  Copyright © 2017年 maopu. All rights reserved.
//  帖子回复详情页

import UIKit

class HotchpotchReplyDetailVC: BaseViewController , MoreToolViewDelegate{
    fileprivate var tableView:UITableView!
    fileprivate var bottomView:SendCommentView!
    fileprivate var moreToolView:MoreToolView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.initView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    fileprivate func initView(){
        self.initNavigationView()
        
        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: AppHeight - 50), style:UITableViewStyle.grouped)
        self.tableView.estimatedRowHeight = 200
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.separatorStyle = .none
        self.tableView.keyboardDismissMode = .onDrag
        self.tableView.tableHeaderView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 0.01))
        self.tableView.tableFooterView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 0.01))
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UINib(nibName: "ReplyItemCell", bundle: nil), forCellReuseIdentifier: "ReplyItemCell")
        self.tableView.register(UINib(nibName: "HomePageListItemCell", bundle: nil), forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView)
        
        self.initBottomView()
    }
    
    fileprivate func initNavigationView(){
        self.navigationItem.title = "帖子详情"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.setBackBarButton(action: #selector(self.goBack), target: self)
        
        let moreBtn = UIButton.init(type: .custom)
        moreBtn.frame = CGRect.init(x: 0, y: 0, width: 16, height: 4)
        moreBtn.setImage(UIImage.init(named: "more_ico"), for: UIControlState())
        moreBtn.addTarget(self, action: #selector(self.showShareView), for: .touchUpInside)
        let moreBarBtn = UIBarButtonItem.init(customView: moreBtn)
        
        let landlordBtn = UIButton.init(type: .custom)
        landlordBtn.frame = CGRect.init(x: 0, y: 0, width: 38, height: 22)
        landlordBtn.setImage(UIImage.init(named: "landlordBtn_ico"), for: UIControlState())
        landlordBtn.addTarget(self, action: #selector(self.lookLandlordClick), for: .touchUpInside)
        let landlordBarBtn = UIBarButtonItem.init(customView: landlordBtn)
        self.navigationItem.rightBarButtonItems = [moreBarBtn , landlordBarBtn]
    }
    
    fileprivate func initBottomView(){
        self.bottomView = SendCommentView(frame: CGRect(x: 0, y: AppHeight - 50, width: AppWidth, height: 50))
        self.bottomView.backgroundColor = UIColor.white
        self.view.addSubview(self.bottomView)
    }
    
    //MARK:查看更多
    @objc fileprivate func showShareView(){
        if self.moreToolView == nil{
            self.moreToolView = MoreToolView.init(isVideo: false)
        }
        self.moreToolView.delegate = self
        self.moreToolView.show()
    }
    //MARK:只看楼主
    @objc fileprivate func lookLandlordClick(){
        Log.shared.printLog("只看楼主")
    }
    
    func moreToolView(moreToolView: MoreToolView, didSelectAt button: UIButton) {
        
    }
}

extension HotchpotchReplyDetailVC:UITableViewDelegate , UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }
        return 3
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0{
            return 10
        }else{
            return 0.01
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomePageListItemCell
            cell.selectionStyle = .none
            cell.refreshCellData(cellType: 1)
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "ReplyItemCell", for: indexPath) as! ReplyItemCell
            cell.selectionStyle = .none
            cell.refreshCellData(indexPathRow: indexPath.row, cellType: VideoReplyCellType.reply , hideUpvoteView: true)
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section  != 0{
            let vc = HotchpotchCommentDetailVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
